<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller\Util;

use App\Controller\AbstractController;
use App\Dao\System\SystemFileDao;
use App\Model\Admin\ConfigValue;
use App\Model\Admin\File;
use App\Model\System\SystemFile;
use App\Model\User;
use App\Service\UserService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Guzzle\ClientFactory;
use Hyperf\HttpMessage\Stream\SwooleFileStream;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Gregwar\Captcha\CaptchaBuilder;
use League\Flysystem\Filesystem;
use Yj\Apidog\Annotation\ApiController;
use Yj\Apidog\Annotation\FormData;
use Yj\Apidog\Annotation\GetApi;
use Yj\Apidog\Annotation\PostApi;


/**
 * @ApiController(tag="文件管理",prefix="util",description="")
 */
class ImageController extends AbstractController
{

    /**
     * @Inject()
     * @var \Hyperf\Filesystem\FilesystemFactory
     */
    protected $filesFactory;

    /**
     * @Notes:【】
     * @User: zwc
     * @Date: 2022-05-24 1:08
     * @GetApi(path="image")
     * @FormData(key="id",rule="Required")
     */
    public function image()
    {
        $id = $this->getData()->get('id');
        $file = App(SystemFileDao::class)->where('id', $id)->first();
        $path = $this->filesFactory->get('local')->read($file->path);
        return $this->response
            ->withAddedHeader('content-type', 'image/png')
            ->withBody(new SwooleStream($path));
    }
}
